#include "head.h"

// 全局变量定义
int STUNUM = 0;
int INDEX = 0;

// 结构体声明
typedef struct {
    char name[50];
    int age;
    float score;
} Student;


Student* createStudent(){
    Student* student = (Student*)malloc(sizeof(Student));
    return student;
}

// 1.学生信息初始化
void initStudent(Student* s, const char* name, int age, float score) {
    strncpy(s->name, name, sizeof(s->name) - 1);
    s->name[sizeof(s->name) - 1] = '\0'; // 确保字符串终止
    s->age = age;
    s->score = score;
    STUNUM++; // 每次初始化学生信息时，学生数量+1
}

// 2.打印学生信息
char* toString(Student* s) {
    static char buffer[100];
    snprintf(buffer, sizeof(buffer), "Name: %s, Age: %d, Score: %.2f", s->name, s->age, s->score);
    return buffer;
}

// 3.创建学生数组
Student** createStudentArray(int size) {
    Student** students = (Student**)malloc(size * sizeof(Student*));
    for (int i = 0; i < size; i++) {
        students[i] = createStudent();
    }
    return students;
}

// 将学生信息添加到学生数组中
void addStudentToArray(Student** students, Student* student) {
    if (INDEX < 0 || INDEX >= STUNUM) {
        printf("Index out of bounds\n");
        return;
    }
    students[INDEX] = student; // 将学生信息添加到数组中
    INDEX++;
}

// 5.获取学生信息   
void printStudentArray(Student** students) {
    for (int i = 0; i < INDEX; i++) {
        printf("%s\n", toString(students[i]));
    }
}

// 4.释放学生数组
void freeStudentArray(Student** students, int size) {
    for (int i = 0; i < size; i++) {
        free(students[i]);
    }
    free(students);
}

// 8.获取学生数量
int getStudentCount() {
    return STUNUM;
}
